using micromsg;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Plugin.Shake.Scene
{
	public class NetSceneShakeImg : NetSceneBaseEx<ShakeImgRequest, ShakeImgResponse, ShakeImgRequest.Builder>
	{
		private const string TAG = "NetSceneShakeImg";
		private const int MAX_TIMES = 10;
		private uint mImgId;
		private uint mImgTotalLenth;
		private uint mStartPos;
		private byte[] mImgbuf;
		private uint mTimesToServer;
		public bool doScene(uint imdId, uint imgTotalLenth)
		{
			Log.smethod_4("NetSceneShakeImg", string.Concat(new object[]
			{
				"doscene in， imdId =",
				imdId,
				"imgTotalLenth",
				imgTotalLenth
			}));
			if (imdId != 0u && imgTotalLenth != 0u)
			{
				this.mImgId = imdId;
				this.mImgTotalLenth = imgTotalLenth;
				this.mStartPos = 0u;
				this.mTimesToServer = 0u;
				this.mImgbuf = new byte[imgTotalLenth];
				this.doSceneEx();
				return true;
			}
			return false;
		}
		private bool doSceneEx()
		{
			Log.smethod_4("NetSceneShakeImg", string.Concat(new object[]
			{
				"dosceneEx in,old imgId = ",
				ConfigMgr.smethod_0<ShakeConfig>().nShakeImgID,
				"new imId = ",
				this.mImgId,
				"totalLength = ",
				this.mImgTotalLenth,
				"startPos = ",
				this.mStartPos
			}));
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.ImgId = this.mImgId;
			this.mBuilder.StartPos = this.mStartPos;
			this.mBuilder.TotalLen = this.mImgTotalLenth;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/shakeimg";
			base.endBuilder();
			this.mTimesToServer += 1u;
			return true;
		}
		protected override void onFailed(ShakeImgRequest request, ShakeImgResponse response)
		{
			Log.smethod_4("NetSceneShakeImg", "onFailed in");
		}
		protected override void onSuccess(ShakeImgRequest request, ShakeImgResponse response)
		{
			Log.smethod_4("NetSceneShakeImg", "onSuccess in");
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneShakeImg", "get shakeImg failed");
				return;
			}
			if (this.mImgTotalLenth != response.TotalLen)
			{
				Log.smethod_1("NetSceneShakeImg", string.Concat(new object[]
				{
					"get shakeImg failed,mImgTotalLenth = ",
					this.mImgTotalLenth,
					"response.TotalLen = ",
					response.TotalLen
				}));
				return;
			}
			if (this.mTimesToServer > 10u)
			{
				Log.smethod_1("NetSceneShakeImg", "reach the max times, mTimesToServer = " + this.mTimesToServer);
				return;
			}
			Buffer.BlockCopy(response.Buffer.Buffer.ToByteArray(), 0, this.mImgbuf, (int)this.mStartPos, response.Buffer.Buffer.ToByteArray().Length);
			this.mStartPos += (uint)response.Buffer.Buffer.ToByteArray().Length;
			if (this.mStartPos < this.mImgTotalLenth)
			{
				this.doSceneEx();
				return;
			}
			ConfigMgr.smethod_0<ShakeConfig>().nShakeImgID = this.mImgId;
			ConfigMgr.save<ShakeConfig>(false);
			ShakeStorage.saveImg(this.mImgbuf);
		}
	}
}
